#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#include <stdbool.h> //c99  not c89???

 
	char ba[18];//={ 'I','I','I','I','I','I','I','I',};
	int size_ba=18; 
	
	char newba[8];
	int size_newba=8;
	
	char fromBa[8];
	int size_fromBa=8;

void print_bytes(char *a,int size ){
printf("\n print\n");
int i=0;
for( i=0;i<size;i++){
printf("%#4x ",(unsigned char)a[i]);

}
printf("\n print end");
}


void long__to__bytes(long a,int size_a ){
long mya=a;
 size_ba=sizeof mya;
 
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
ba[i]=(unsigned char)aa[i];//(unsigned char)aa[i];
//printf("%#4x ",(unsigned char)aa[i]);
}
//memcpy(ba,aa,size_a);

}

long bytes__to__long(){
long mya=0;
int size_a=0;
 size_a=sizeof mya;
 	printf("\n here,long size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
aa[i]=fromBa[i];//(unsigned char)aa[i]; 
}
 return mya;

}



void float__to__bytes(float a,int size_a ){
float mya=a;
 size_ba=size_a;//sizeof mya;
 	printf("\n here,float size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
ba[i]=aa[i];//(unsigned char)aa[i];
//printf("%#4x ",(unsigned char)aa[i]);
}
//memcpy(ba,aa,size_a);

}

float bytes__to__float(){
float mya=0;
int size_a=0;
 size_a=sizeof mya;
 	printf("\n here,float size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
aa[i]=fromBa[i];//(unsigned char)aa[i]; 
}
 return mya;

}


void double__to__bytes(double a,int size_a ){
double mya=a;
 size_ba=sizeof mya;
 	printf("\n here,double size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
ba[i]=aa[i];//(unsigned char)aa[i];
//printf("%#4x ",(unsigned char)aa[i]);
}
//memcpy(ba,aa,size_a);

}

double bytes__to__double(){
double mya=0;
int size_a=0;
 size_a=sizeof mya;
 	printf("\n here,double size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
aa[i]=fromBa[i];//(unsigned char)aa[i]; 
}
 return mya;

}





void int__to__bytes(int a,int size_a ){
int mya=a;
 size_ba=sizeof mya;
 	printf("\n here,int size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
ba[i]=aa[i];//(unsigned char)aa[i];
//printf("%#4x ",(unsigned char)aa[i]);
}
//memcpy(ba,aa,size_a);

}

int bytes__to__int(){
int mya=0;
int size_a=0;
 size_a=sizeof mya;
 	printf("\n here,int size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
aa[i]=fromBa[i];//(unsigned char)aa[i]; 
}
 return mya;

}



void bool__to__bytes(bool a,int size_a ){
bool mya=a;
 size_ba=sizeof mya;
 	printf("\n here,bool size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
ba[i]=aa[i];//(unsigned char)aa[i];
//printf("%#4x ",(unsigned char)aa[i]);
}
//memcpy(ba,aa,size_a);

} 

bool bytes__to__bool(){
bool mya=true;
int size_a=0;
 size_a=sizeof mya;
 	printf("\n here,bool size = %d", size_a);
char *aa=(char *)&mya;
int i=0;
for(i=0;i<size_a;i++){
aa[i]=fromBa[i];//(unsigned char)aa[i]; 
}
 return mya;

} 
 
void bytearray_conjunct(){
 //
	 int jj;
	 for (jj=size_ba;jj<size_ba+size_newba;jj++) {
		 ba[jj]=newba[jj-size_ba];	 
	 }
	 //
	  size_ba=size_ba+size_newba;

}
 
 
 
void  bytearray_insert(int index_start) 
 {   int offset=size_newba; 
	 int j;
	 for (j=0;j<index_start;j++) {
		 ba[j]=ba[j];	 
	 }
	  
	 for (j=size_ba-1;j>=index_start;j--) {
		 ba[offset+j]=ba[j];	 
	 }  
      
	 //
	 int jj;
	 for (jj=index_start;jj<index_start+size_newba;jj++) {
		 ba[jj]=newba[jj-index_start];	 
	 }
	 //
	  size_ba=size_ba+size_newba;
	 ///////////return ba;
}

  //like subString 
void bytearray_delete(int index_start,int index_next_start) {
		int offset=-(index_next_start-index_start);
	
		 if (index_next_start>=size_ba) {index_next_start=size_ba;}
		 
	     if ((index_start<size_ba)&&(index_next_start<=size_ba)&&index_start<index_next_start){
	      
		   int j;
	      for (j=0;j<index_start;j++) {
		   ba[j]=ba[j];	 
     	  } 
          for ( j=index_next_start;j<size_ba;j++) {
		   ba[offset+j]=ba[j];	 
    	  }  
		 }
	     else {
		 
		 }
		 size_ba=size_ba+offset;
	  } 
void  bytearray_insteadof(int index_start,int index_next_start) {
	 int offset=size_newba-(index_next_start-index_start);
	 //
	 //if (offset==0){   //if  do,can be more rapid,
	 //}else{
	 //////////byte[] ba=new byte[ba_SRC.length+offset];
	
	 int j;
	 for (j=0;j<index_start;j++) {
		 ba[j]=ba[j];	 
	 }
	 //
	 if (offset>0){
	 	int j;
	 for (j=size_ba-1;j>=index_next_start;j--) {
		 ba[offset+j]=ba[j];	 
	 }  
     }else{
     	int j;
      for ( j=index_next_start;j<size_ba;j++) {
		 ba[offset+j]=ba[j];	 
	 } 	

     }
	 //
	 int jj;
	 for (jj=index_start;jj<index_start+size_newba;jj++) {
		 ba[jj]=newba[jj-index_start];	 
	 }
	 //
	  size_ba=size_ba+offset;
	 ///////////return ba;
}




 
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{ 
 printf("\n\n test time:");
	time_t t;
	t = time(NULL);
 
	int ii = time(&t);
	printf("\n ii = %d", ii);
	print_bytes((char *)&ii,sizeof ii);
	//system("pause");  
////////////////////////	

printf("\n\n test long:\n");
long a=127;
	printf("\n long = %d\n", a);
int size=sizeof a;
char *aa=(char *)&a;
int i=0;
for(i=0;i<size;i++){
printf("%#4x ",(unsigned char)aa[i]); 
}

 print_bytes((char *)&a,sizeof a);
//
printf("\n-------------");
 long__to__bytes(a,4 );
 print_bytes(ba,size_ba );
////////////////////


printf("\n\n test float:");
float f=1;  
 print_bytes((char *)&f,sizeof f); 
//
printf("\n-------------");
 float__to__bytes(f,4 );
 print_bytes(ba,size_ba );
////////////////////


printf("\n\n test double:");
double df=2;//1;  
 print_bytes((char *)&df,sizeof df); 
//
printf("\n-------------");
 double__to__bytes(df,sizeof df );
 print_bytes(ba,size_ba );
////////////////////
 
printf("\n\n test int:");
int iii=65535;  
 print_bytes((char *)&iii,sizeof iii); 
//
printf("\n-------------");
 int__to__bytes(iii,sizeof iii );
 print_bytes(ba,size_ba );
////////////////////

 
printf("\n\n test boolean:");
bool mybool=false;//true;  
 print_bytes((char *)&mybool,sizeof mybool); 
//
printf("\n-------------");
 bool__to__bytes(mybool,sizeof mybool );
 print_bytes(ba,size_ba );
////////////////////

 

printf("\n\n test fromBa to int:");
fromBa[0]=255;
fromBa[1]=255;
fromBa[2]=255;
fromBa[3]=255;
int aaa= bytes__to__int(); 
	printf("\n int = %d\n", aaa);
////////////////////

printf("\n\n test fromBa to long:");
fromBa[0]=255;
fromBa[1]=255;
fromBa[2]=255;
fromBa[3]=128;//255;
fromBa[4]=255;
fromBa[5]=255;
fromBa[6]=255;
fromBa[7]=127;//255;

long laaa= bytes__to__long(); 
	printf("\n long = %d\n", laaa);
////////////////////
 

printf("\n\n test fromBa to double:");
fromBa[0]=0;
fromBa[1]=0;
fromBa[2]=0;
fromBa[3]=0;
fromBa[4]=0;
fromBa[5]=0;
fromBa[6]=0;//0xF0;
fromBa[7]=0x40;//0x3F;
double daaa= bytes__to__double(); 
	printf("\n double = %lf\n", daaa);
 	printf("\n double = %e\n", daaa);/// 
////////////////////
 

printf("\n\n test fromBa to float:");
fromBa[0]=0;
fromBa[1]=0;
fromBa[2]=0x80;
fromBa[3]=0x3F; 
double faaa= bytes__to__float(); 
	printf("\n float = %f\n", faaa);
 	printf("\n float = %e\n", faaa);/// 
////////////////////
 

printf("\n\n test fromBa to bool:");
fromBa[0]=0;
bool baaa= bytes__to__bool(); 
	printf("\n bool = %d\n", baaa); // as  int ,for out print 

////////////////////

printf("\n\n test bytearray_insteadof:");
ba[0]=0x30;
ba[1]=0x31;
ba[2]=0x32;
ba[3]=0x33; 
ba[4]=0x34;
ba[5]=0x35;
ba[6]=0x36;
ba[7]=0x37;
size_ba=8;

 
  
 newba[0]=0x40;
 newba[1]=0x41;
  newba[2]=0x42;
 size_newba=3;

printf("%s\n",ba);
printf("%s\n",newba);



 bytearray_insteadof(2,4);
 ba[size_ba]=0;//0 ,end of  string!!!
printf("%s\n",ba);
///


////////////////////

printf("\n\n test bytearray_delete:");
ba[0]=0x30;
ba[1]=0x31;
ba[2]=0x32;
ba[3]=0x33; 
ba[4]=0x34;
ba[5]=0x35;
ba[6]=0x36;
ba[7]=0x37;
ba[8]=0;
size_ba=8; 

printf("%s\n",ba);  

 bytearray_delete(2,4);
 ba[size_ba]=0;//0 ,end of  string!!!
printf("%s\n",ba);
///

////////////////////

printf("\n\n test bytearray_insert:");
ba[0]=0x30;
ba[1]=0x31;
ba[2]=0x32;
ba[3]=0x33; 
ba[4]=0x34;
ba[5]=0x35;
ba[6]=0x36;
ba[7]=0x37;
ba[8]=0;
size_ba=8; 

printf("%s\n",ba);  

 
 newba[0]=0x40;
 newba[1]=0x41;
 newba[2]=0x42;
 size_newba=3;


 bytearray_insert(8);
 ba[size_ba]=0;//0 ,end of  string!!!
printf("%s\n",ba);
///

  
////////////////////

printf("\n\n test bytearray_conjunct:");
ba[0]=0x30;
ba[1]=0x31;
ba[2]=0x32;
ba[3]=0x33; 
ba[4]=0x34;
ba[5]=0x35;
ba[6]=0x36;
ba[7]=0x37;
ba[8]=0;
size_ba=8; 

printf("%s\n",ba);  
printf("%s\n",newba);
 
 newba[0]=0x40;
 newba[1]=0x41;
 newba[2]=0x42;
 size_newba=3;


 bytearray_conjunct();
 ba[size_ba]=0;//0 ,end of  string!!!
printf("%s\n",ba);
///

return 0; 
}

 


